package com.ltt.service.impl;

import com.ltt.dao.pojo.Blog;
import com.ltt.dao.pojo.CommentOne;
import com.ltt.dao.pojo.CommentTwo;
import com.ltt.dao.pojo.User;
import com.ltt.dao.vo.CommentVo;
import com.ltt.dao.vo.params.NumsParams;
import com.ltt.mapper.BlogMapper;
import com.ltt.mapper.CommentMapper;
import com.ltt.mapper.UserInfoMapper;
import com.ltt.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/1 21:46
 */
@Service
public class CommentServiceImpl  implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<CommentVo> getAllComment(int blog_id) {
        List<CommentOne> commentOneById = commentMapper.getCommentOneById(blog_id);

        List<CommentVo> commentVoList = new ArrayList<>();
        for (CommentOne commentOne : commentOneById) {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(commentOne,commentVo);
            commentVoList.add(commentVo);
        }

        for (CommentVo commentVo : commentVoList) {
            User userById = userInfoMapper.findUserById(commentVo.getUserId());
            commentVo.setImg(userById.getUserPic());
            commentVo.setName(userById.getNickName());
            int levels = commentVo.getLevels();
            if(levels >= 1){
                List<CommentTwo> commentTwoById = commentMapper.getCommentTwoById(commentVo.getId());


                List<CommentVo> commentVoListTwo = new ArrayList<>();
                for (CommentTwo commentTwo : commentTwoById) {
                    CommentVo commentVoTwo = new CommentVo();
                    BeanUtils.copyProperties(commentTwo,commentVoTwo);
                    commentVoListTwo.add(commentVoTwo);
                }
                for (CommentVo vo : commentVoListTwo) {
                    User userById1 = userInfoMapper.findUserById(vo.getUserId());
                    vo.setName(userById1.getNickName());
                    vo.setImg(userById1.getUserPic());
                    System.out.println(vo);
                }
                commentVo.setCommentVoList(commentVoListTwo);
            }
        }
        return commentVoList;
    }

    @Override
    public void insertCommentOne(CommentOne commentOne) {
        commentMapper.insertCommentOne(commentOne);
        Blog blog = blogMapper.findBlog(commentOne.getBlogId());
        NumsParams numsParams = new NumsParams();
        numsParams.setCommentNums(blog.getCommentNums()+1);
        numsParams.setBlogId(commentOne.getBlogId());
        blogMapper.updateCommentNums(numsParams);
    }

    @Override
    public void insertCommentTwo(CommentTwo commentTwo) {
        commentMapper.updateCommentTwo(commentTwo.getParentId());
        commentMapper.insertCommentTwo(commentTwo);

        int blogIdByCommentId = commentMapper.getBlogIdByCommentId(commentTwo.getParentId());
        Blog blog = blogMapper.findBlog(blogIdByCommentId);
        NumsParams numsParams = new NumsParams();
        numsParams.setCommentNums(blog.getCommentNums()+1);
        numsParams.setBlogId(blog.getId());
        blogMapper.updateCommentNums(numsParams);
    }
}
